package Try;

/**
 * Created by dell on 2017/5/14.
 */
import java.util.*;

/**
 * Created by 蜡笔小新丶 on 2017/5/2.
 */

public class Original {
    private Stack<String> stack1;
    private List<String> list1;
    private String message,Message="";

    public Original() {
        stack1 = new Stack<String>();
        list1 = new ArrayList<String>();
    }

    public void evaluate(String expr) {
        int op1, op2, result = 0;
        String token;
        StringTokenizer tokenizer = new StringTokenizer(expr);

        while (tokenizer.hasMoreTokens()) {
            token = tokenizer.nextToken();

            if (token.equals("("))
                stack1.push(token);
            else if (token.equals("+") || token.equals("-")) {
                if(!stack1.empty()) {
                    if (stack1.peek().equals("*") || stack1.peek().equals("/")){
                        list1.add(stack1.pop());
                        stack1.push(token);
                    } else
                        stack1.push(token);
                }
                else {
                    stack1.push(token);
                }
            }
            else if (token.equals("*") || token.equals("/")) {
                if(!stack1.empty()){
                    if(stack1.peek().equals("*")||stack1.peek().equals("/")) {
                        list1.add(stack1.pop());
                    }
                }
                stack1.push(token);
            }
            else if (token.equals(")")) {
                while (true) {
                    String A = stack1.pop();
                    if (!A.equals( "("))
                        list1.add(A);
                    else break;
                }
            }else list1.add(token);
        }
        while (!stack1.empty()) {
            list1.add(stack1.pop());
        }
        ListIterator<String > li = list1.listIterator();
        while (li.hasNext()) {
            Message += li.next() + " ";
            li.remove();
        }
        message = Message;
    }

    public String getMessage(){
        return message;
    }
}
